package Panel;

import Bean.Out;
import Dao.OutDao;
import Utils.MainTopView;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.util.List;

public class MainPanel {
    private JoinDepotPanel four;
    private JTable table;
    private JScrollPane scrollPane;
    //主面板测试主方法
   static JPanel right = new JPanel();
    JLabel tit = new JLabel("基本档案");
    JLabel location = new JLabel("您当前的位置是:"+tit.getText());
//    public static void main(String[] args) {
//        MainPanel mainPanel = new MainPanel();
////        mainPanel.init();
//    }
    public MainPanel (){
        System.out.println("主面板初始化");
        init();
    }

    private void init(){
        JFrame f = new JFrame("超市管理系统");
        f.setLayout(null);
        f.setBounds(100,100,900,600);
        //顶部面板
        JPanel top = new JPanel();
        top.setLayout(null);
        top.setBounds(0,0,900,100);
        //左部面板
        JPanel left = new JPanel();
        left.setLayout(null);
        left.setBounds(0,100,220,500);
        left.setBackground(Color.pink);
        //右部面板

        right.setLayout(null);
        right.setBounds(220,100,680,500);
        right.setBackground(Color.orange);

        JLabel title = new JLabel("腾宇超市管理系统");
        JLabel title2 = new JLabel("提高效率  有效管理");
        title.setBounds(0,0,120,100);
        title2.setBounds(750,0,120,100);

        /**
         * 设置top 中间组件
         */
        JPanel scroll = new JPanel();
        scroll.setLayout(null);
        scroll.setPreferredSize(new Dimension(660,80));
        JButton[] btns = MainTopView.getBtns();
        for (int i = 0; i < btns.length; i++) {
            scroll.add(btns[i]);
            btns[i].addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String text = e.paramString().split(",")[1].split("=")[1];
                        setTit(text);
                    if("基本档案".equals(text)){
                        BasicArchivesPanel fir = new BasicArchivesPanel();
                        ContentRepaint(fir);
                    }else if("采购订货".equals(text)){
                        OrderGoodsPanel sec = new OrderGoodsPanel();
                        ContentRepaint(sec);
                    }else if("仓库入库".equals(text)){
                        JoinDepotPanel thi =new JoinDepotPanel();
                        ContentRepaint(thi);
                    }else if("仓库出库".equals(text)){
                        JPanel four = fourPaenl();
                        ContentRepaint(four);
                    }else if("人员管理".equals(text)){
                        PersonnelManagementPanel fif = new PersonnelManagementPanel();
                        ContentRepaint(fif);
                    }else if("部门管理".equals(text)){
                        DepartmentManagementPanel six = new DepartmentManagementPanel();
                        ContentRepaint(six);
                    }
                }
            });
        }

        JScrollPane jp = new JScrollPane(scroll,ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        JScrollBar hb = jp.getHorizontalScrollBar();
        jp.setBounds(180,10,450,80);


        //左右按钮
        JButton leftBtn = new JButton("<");
        JButton rightBtn = new JButton(">");
        leftBtn.setBounds(110,35,50,30);
        rightBtn.setBounds(660,35,50,30);
        top.add(leftBtn);
        top.add(rightBtn);
        //左右按钮控制横向滚动实现
        leftBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                hb.setValue(hb.getValue()-20);
            }
        });
        rightBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                hb.setValue(hb.getValue()+20);
            }
        });

        top.add(title);
        top.add(title2);
        top.add(jp);
        top.setBackground(Color.red);
        top.setVisible(true);

        /**
         * 设置左边组件
         */


        //时钟面板
        ClockPanel clock = new ClockPanel();
        //日历面板
        CalenderPanel calender = null;
        try {
            calender = new CalenderPanel();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        left.add(calender);
        left.add(clock);
        left.setVisible(true);

        /**
         * 设置右边组件
         */


        tit.setBounds(30,30,80,30);
        location.setBounds(60,60,150,30);
        BasicArchivesPanel contentPanel = new BasicArchivesPanel();
        right.add(contentPanel);
        right.add(tit);
        right.add(location);
        right.setVisible(true);


        f.add(top);
        f.add(left);
        f.add(right);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

    public void ContentRepaint(JPanel content){
        right.removeAll();
        right.setVisible(false);
        right.add(content);
        right.add(tit);
        right.add(location);
        right.setVisible(true);
    }

    public void setTit(String text){
        tit.setText(text);
        location.setText("您当前的位置是:"+text);
    }

    /**
     * 因为和入库面板相似 所以new一个入库面板 然后修改对应的属性
     * @return
     */
    public JPanel fourPaenl(){
        four = new JoinDepotPanel();
        //获取需要修改的组件
        JLabel depot = (JLabel) four.getComponentAt(90,0);
        JLabel outDepotTime = (JLabel) four.getComponentAt(350, 0);
        JComboBox depotCb = (JComboBox) four.getComponentAt(150,5);
        JTextField depotTimeI = (JTextField) four.getComponentAt(400,5);
        JButton q = (JButton) four.getComponentAt(580, 5);
        scrollPane = (JScrollPane) four.getComponentAt(40, 80);
        JButton add = (JButton) four.getComponentAt(150, 330);
        JButton mod = (JButton) four.getComponentAt(300, 330);
        JButton del = (JButton) four.getComponentAt(450, 330);
        four.remove(q);
        four.remove(add);
        four.remove(mod);
        four.remove(del);
        JButton outQ = new JButton("搜索");
        JButton outAdd = new JButton("添加");
        JButton outMod = new JButton("修改");
        JButton outDel= new JButton("删除");
        outQ.setBounds(580,5,80,20);
        outAdd.setBounds(150, 330,80,20);
        outMod.setBounds(300, 330,80,20);
        outDel.setBounds(450, 330,80,20);
        four.add(outQ);
        four.add(outAdd);
        four.add(outMod);
        four.add(outDel);
        depot.setText("仓库");
        outDepotTime.setText("出库时间");
        //设置数据
        List<Out> list = OutDao.getOutList();
        String[] oidList = new String[list.size()];
        for (int i = 0; i <list.size() ; i++) {
            oidList[i] = String.valueOf(list.get(i).getDid());
        }
        table = new JTable(getTableList(OutDao.getOutList()),new String[]{"编号","仓库编号", "货品名称", "出库时间", "重量", "备注"});
        depotCb.setModel(new DefaultComboBoxModel(oidList));
        scrollPane.setViewportView(table);

        //响应事件
        outQ.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String item = (String) depotCb.getSelectedItem();
                String time = depotTimeI.getText();
                List<Out> out = OutDao.queryInfoByDid(new String[]{item, time});
                tableReDraw(out);
            }
        });

        outAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                showModel("添加出库信息", "添加");
            }
        });

        outMod.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                showModel("修改出库信息", "修改");
            }
        });

        outDel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                showModel("删除出库信息","删除");
            }
        });
        return four;
    }

    public String[][] getTableList(List<Out> l){
        List<Out> list = l;
        String[][] tableList = new String[list.size()][6];
        for (int i = 0; i < list.size(); i++) {
            tableList[i][0] = String.valueOf(list.get(i).getId());
            tableList[i][1] = String.valueOf(list.get(i).getDid());
            tableList[i][2] = String.valueOf(list.get(i).getName());
            tableList[i][3] = String.valueOf(list.get(i).getDepotTime());
            tableList[i][4] = String.valueOf(list.get(i).getWeight());
            tableList[i][5] = String.valueOf(list.get(i).getRemark());
        }
        return tableList;
    }

    public JFrame showModel(String title,String buttonName){
        JFrame j = new JFrame(title);
        j.setLayout(null);
        j.setBounds(450,300,600,300);
        JLabel id = new JLabel("编号");
        JLabel did = new JLabel("仓库编号");
        JLabel name = new JLabel("货品名称");
        JLabel time = new JLabel("出库时间");
        JLabel wight = new JLabel("重量");
        JLabel remark = new JLabel("备注");
        id.setBounds(0,0,100,20);
        did.setBounds(400,0,100,20);
        name.setBounds(0,100,100,20);
        time.setBounds(200,100,100,20);
        wight.setBounds(400,100,100,20);
        remark.setBounds(0,200,100,20);
        j.add(id);
        if(!"删除".equals(buttonName)){
            j.add(did);
            j.add(name);
            j.add(time);
            j.add(wight);
            j.add(remark);
        }


        //输入框
        JTextField idI = new JTextField();
        JTextField didI = new JTextField();
        JTextField nameI = new JTextField();
        JTextField timeI = new JTextField();
        JTextField weightI = new JTextField();
        JTextField remarkI = new JTextField();
        idI.setBounds(50,0,120,20);
        didI.setBounds(450,0,120,20);
        nameI.setBounds(50,100,120,20);
        timeI.setBounds(250,100,120,20);
        weightI.setBounds(450,100,120,20);
        remarkI.setBounds(50,200,120,20);
        j.add(idI);
        if(!"删除".equals(buttonName)){
            j.add(didI);
            j.add(nameI);
            j.add(timeI);
            j.add(weightI);
            j.add(remarkI);

        }

        //按钮
        JButton modelAdd = new JButton(buttonName);
        JButton modelColse = new JButton("关闭");
        modelAdd.setBounds(200,220,80,20);
        modelColse.setBounds(400,220,80,20);
        j.add(modelAdd);
        j.add(modelColse);

        //模态框按钮事件
        modelAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                Out out = new Out();
                Boolean isSucc = null;
                try {
                    //判断id是否重复
                    List<Integer> id1 = OutDao.findId();
                    if(!id1.contains(Integer.parseInt(idI.getText())) && "添加".equals(buttonName)){
                        out.setId(Integer.parseInt(idI.getText()));
                    }else if(id1.contains(Integer.parseInt(idI.getText())) && "添加".equals(buttonName)){
                        JOptionPane.showMessageDialog(j,"请不要输入相同的编号");
                        return;
                    }else  if(id1.contains(Integer.parseInt(idI.getText())) && "修改".equals(buttonName)){
                        out.setId(Integer.parseInt(idI.getText()));
                    }else if(!id1.contains(Integer.parseInt(idI.getText())) && "修改".equals(buttonName)){
                        JOptionPane.showMessageDialog(j,"请输入已经存在的编号");
                        return;
                    }else if(id1.contains(Integer.parseInt(idI.getText())) && "删除".equals(buttonName)){
                        out.setId(Integer.parseInt(idI.getText()));
                    }else {
                        JOptionPane.showMessageDialog(j,"请输入已经存在的编号");
                        return;
                    }
                    if(!"删除".equals(buttonName)){
                        out.setDid(Integer.parseInt(didI.getText()));
                        out.setName(nameI.getText());
                        out.setDepotTime(timeI.getText());
                        out.setWeight(Float.parseFloat(weightI.getText()));
                        out.setRemark(remarkI.getText());
                    }
                }catch (NumberFormatException e){
                    JOptionPane.showMessageDialog(j,"输入类型错误 请重试");
                    return;
                }
                if("添加".equals(buttonName)){
                    isSucc = OutDao.addInfo(out);
                }else if("修改".equals(buttonName)){
                    isSucc = OutDao.updateOutInfo(out);
                }else if("删除".equals(buttonName)){
                    isSucc = OutDao.deleteOutInfo(out);
                }
                if(isSucc!= null && isSucc){
                    JOptionPane.showMessageDialog(j,buttonName+"成功");
                    tableReDraw(OutDao.getOutList());

                }else {
                    JOptionPane.showMessageDialog(j,buttonName+"失败 请重试");
                }
            }
        });
        modelColse.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                j.dispose();
            }
        });
        j.setVisible(true);
        return j;
    }

   public void tableReDraw(List<Out> out){
       four.setVisible(false);
       four.remove(scrollPane);
       table.setModel(new DefaultTableModel(getTableList(out),new String[]{"编号", "仓库编号", "货品名称", "出库时间", "重量", "备注"}));
       scrollPane.setViewportView(table);
       four.add(scrollPane);
       four.setVisible(true);
    }
}
